import entity.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

public class Demo {

public static void main(String args[]){

    Demo demo = new Demo();
    
//    ContenidoWeb c1 = new ContenidoWeb();
//    c1.setTitulo("titulo c2");
//    c1.setFecha(new Date(System.currentTimeMillis()));
    
//    demo.persist(c1);
    
    Autor autor1 = new Autor();
    autor1.setNombre("nombre autor1");
    
    Tag t1 = new Tag();
    t1.setValor("tag1");
    Tag t2 = new Tag();
    t2.setValor("tag2");
    
    EntradaBlog e =  new EntradaBlog();
    e.setContenido("esto es el contenido");
    e.setAutor(autor1);
    e.agregarTag(t1);
    e.agregarTag(t2);
    
    demo.persist(t1);
    demo.persist(t2);
    
    demo.persist(autor1);
    demo.persist(e);
    
    PaginaWeb p1 = new PaginaWeb();
    p1.setTitulo("pag 1");
    p1.setFecha(new Date(System.currentTimeMillis()));
    p1.setContenido(("<html> <title>titulo</title> <body>esto es el contenido</body> </html>").getBytes());
    
    //demo.persist(p1);
    demo.crearPaginaWeb(p1);
    
    String ret;
    ret = getPaginaWeb(p1.getId()).getTitulo();
    System.out.println(ret);
    
    System.out.println(p1.getId().toString());
    demo.eliminarPaginaWeb(p1.getId());
    
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
String desdeString = "2012-06-04";
Date desdeDT = null;
try {
    desdeDT = formato.parse(desdeString);

} catch (ParseException ex) {
    ex.printStackTrace();
}


String hastaString = "2012-06-07";
Date hastaDT = null;
try {
    hastaDT = formato.parse(hastaString);

} catch (ParseException ex) {
    ex.printStackTrace();
}

//    
//    for (PaginaWeb p : demo.listaDePaginasWeb()){
//        System.out.println(p.getTitulo());
//    }
//
//        
//   
//    for (PaginaWeb p01 : demo.listaDePaginasWebPorTitulo("pag 2")){
//        System.out.println(p01.getTitulo());
//    }

        
    for (PaginaWeb p02 : demo.listaDePaginasWebPorFecha(desdeDT, hastaDT)){
            System.out.println(p02.getTitulo() + p02.getId().toString());
    }
   
}

public void  crearPaginaWeb(PaginaWeb pagina){
    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
    EntityManager em = emf.createEntityManager();
    try{
        em.getTransaction().begin();
        em.persist(pagina);
        em.getTransaction().commit();
    }finally{
        em.close();
    }
}

public void eliminarPaginaWeb(long id){
    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
    EntityManager em = emf.createEntityManager();
    try{
        em.getTransaction().begin();
        PaginaWeb paginaWeb = em.find(PaginaWeb.class, id);
        em.remove(paginaWeb);
        em.getTransaction().commit();
    }finally{
        em.close();
    }
}


public static PaginaWeb getPaginaWeb(long id){
    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
    EntityManager em = emf.createEntityManager();
    try{
        return em.find(PaginaWeb.class, id);
    }finally{
        em.close();
    }
}

public static List<PaginaWeb> listaDePaginasWeb(){
        EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
        EntityManager em = emf.createEntityManager();

        TypedQuery<PaginaWeb> query =
            em.createQuery("SELECT p FROM PaginaWeb p", PaginaWeb.class);
        List<PaginaWeb> results = query.getResultList();

        return results;
}
public static List<PaginaWeb> listaDePaginasWebPorTitulo(String unTitulo){
        EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
        EntityManager em = emf.createEntityManager();

        TypedQuery<PaginaWeb> query =
            em.createQuery("SELECT p FROM PaginaWeb p WHERE p.titulo = ?1" , PaginaWeb.class);
            query.setParameter(1, unTitulo);
        List<PaginaWeb> results = query.getResultList();

        return results;
}

public static List<PaginaWeb> listaDePaginasWebPorFecha(Date desde, Date hasta){
        EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
        EntityManager em = emf.createEntityManager();

        TypedQuery<PaginaWeb> query =
            em.createQuery("SELECT p FROM PaginaWeb p WHERE p.fecha >= ?1 and p.fecha < ?2" , PaginaWeb.class);
            query.setParameter(1, desde);
            query.setParameter(2, hasta);
        List<PaginaWeb> results = query.getResultList();

        return results;
}



//
//public static PaginaWeb getPaginaWebPorTitulo(long id){
//    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
//    EntityManager em = emf.createEntityManager();
//    try{
//        return em.
//    }finally{
//        em.close();
//    }
//}



public void persist(Object object) {

    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("testJPAPU");
    EntityManager em = emf.createEntityManager();
    
    em.getTransaction().begin();
    try {
    em.persist(object);
    em.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
    em.getTransaction().rollback();
    } finally {

        em.close();
    }
}
}
